<?php
namespace Modules\Admin\Model\Dao\Api;

use S\Exception;
use S\Http;
use S\Config;

/**
 * Class Util
 */
class Util {
    
    const ERR_CODE_SUCCESS = "0000";  //成功响应码

    private $code;
    private $message;

    /**
     * 发送请求
     *
     * @param $uri
     * @param array $params
     * @param int $timeout default 10
     *
     * @return mixed
     * @throws \S\Exception
     */
    public function request($uri, array $params, $timeout = 10){
        $url = \S\Config::confServer('request.ucenter.inner_host'); //用内网域名
        $options = array(
            'timeout' => $timeout,
            'headers' => array(
                'Content-Type' => 'text/plain'
            )
        );
        $system_id = Config::confServer('request.ucenter.system_id');
        $params['systemId'] = $system_id;
        $http = new Http($url);
        $response = $http->request(Http::METHOD_GET, $uri, $params, $options);
        
        if (!$response) {
            throw new Exception("empty response from user", 5200001);
        }
        $result = json_decode($response, true);
        if (empty($result)) {
            \S\Log\Logger::getInstance()->error(array($response));
            throw new Exception("error format response from user", 5200002);
        }
        if (self::ERR_CODE_SUCCESS != $result["resultCode"]) {
            $this->code = $result['resultCode'];
            $this->message = $result['resultMsg'];
            return false;
        }else{
            $this->code = $result['resultCode'];
            $this->message = $result['resultMsg'];
            unset($result['resultCode']);
            unset($result['resultMsg']);
            return $result;
        }
    }

    public function getCode(){
        return $this->code;
    }

    public function getMessage(){
        return $this->message;
    }

}